package open.tech.dawn.admin.controller;

import open.tech.dawn.admin.config.ApplicationConfig;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class PathController {

    @Resource
    private ApplicationConfig applicationConfig;

    @GetMapping({"login.html"})
    public String login(HttpServletRequest request, Model model) {
        insertModel(request, model);
        return "/login.html";
    }

    @GetMapping({"", "/index.html"})
    public String index(HttpServletRequest request, Model model) {
        insertModel(request, model);
        return "/index.html";
    }

    @GetMapping({"/404.html"})
    public String error(HttpServletRequest request, Model model) {
        insertModel(request, model);
        return "/404.html";
    }

    @GetMapping({"/view.html"})
    public String view(HttpServletRequest request, Model model) {
        insertModel(request, model);
        return "/view.html";
    }


    @GetMapping("/assets/common/js/dawn.js")
    public String dawnJS(HttpServletRequest request, Model model) {
        insertModel(request, model);
        return "/assets/common/js/dawn.js";
    }

    @GetMapping("/views/**")
    public String views(HttpServletRequest request, Model model) {
        insertModel(request, model);
        return request.getServletPath();
    }

    private void insertModel(HttpServletRequest request, Model model) {
        model.addAttribute("app", applicationConfig);
        model.addAttribute("ctxPath", request.getContextPath());
    }

}
